﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace HeadLinx.AD.Explorer
{
    /// <summary>
    /// Interaction logic for SetRunningAsDialog.xaml
    /// </summary>
    public partial class SetRunningAsDialog : Window
    {
        public SetRunningAsDialog()
        {
            InitializeComponent();
            UpdateControls();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            txtRunAsUser.Focus();
            string asMyself = chkAsMyself.Content as string;
            chkAsMyself.Content = String.Format(asMyself, Environment.UserName);
        }

        private void UpdateControls()
        {
            if (chkAsMyself.IsChecked == true)
            {
                txtRunAsUser.IsEnabled = false;
            }
            else
            {
                txtRunAsUser.IsEnabled = true;
            }
        }

        private void chkAsMyself_Click(object sender, RoutedEventArgs e)
        {
            UpdateControls();

            if (chkAsMyself.IsChecked == false)
            {
                txtRunAsUser.Focus();
            }
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            if (chkAsMyself.IsChecked == true || String.IsNullOrEmpty(txtRunAsUser.Text))
            {
                LocalUtility.SetCurrentUserAlias(null);
            }
            else
            {
                LocalUtility.SetCurrentUserAlias(txtRunAsUser.Text);
            }

            this.DialogResult = true;
            this.Close();
        }
    }
}
